package com.globaldom.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.globaldom.model.MilResource;
import com.globaldom.utils.Constants;
import com.globaldom.utils.PMF;
import com.globaldom.utils.SessionManager;

@Controller
@RequestMapping(value = "/getallmilres")
public class MilResourceGetAllController {
	public MilResourceGetAllController() {
	}

	@RequestMapping(value = "/getall", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, String> getAllMilitary(HttpSession session, HttpServletRequest request) {

		Map<String, String> map = new HashMap<String, String>();
		String personID = SessionManager.getID(request, session);
		if(null==personID) 
			{
			map.put(Constants.STATUS, "-ER");
			map.put(Constants.CODE, "401");
			return map;
			}
		
		PersistenceManager pm = PMF.get().getPersistenceManager();

		try {

			Query query = pm.newQuery(MilResource.class, "person==pid");
			query.declareParameters("String pid");
			List<MilResource> resources = (List<MilResource>) query.execute(personID);
			for (MilResource milResource : resources) {
				map.put("res" + milResource.getUnitType(), String.valueOf(milResource.getMilAvailable()));

			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			pm.close();
		}
		return map;

	}

}
